package com.clound.order.converter;

import com.clound.order.dataObject.OrderDetail;
import com.clound.order.dto.OrderDTO;
import com.clound.order.enums.ResultEnum;
import com.clound.order.exception.OrderException;
import com.clound.order.form.OrderForm;
import com.google.common.collect.Lists;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.util.List;

/**
 * Created by zzy on 2018/4/7.
 */
public class OrderForm2DTO {

    public static OrderDTO convert(OrderForm orderForm){
        OrderDTO orderDTO=new OrderDTO();
        orderDTO.setBuyerName(orderForm.getName());
        orderDTO.setBuyerAddress(orderForm.getAddress());
        orderDTO.setBuyerPhone(orderForm.getPhone());
        orderDTO.setBuyerOpenid(orderForm.getOpenid());

        List<OrderDetail> orderDetailList= Lists.newArrayList();
        Gson gson=new Gson();
        try {
            orderDetailList= gson.fromJson(orderForm.getItems(),new TypeToken<List<OrderDetail>>(){}.getType());
        }catch (Exception e){
            throw new OrderException(ResultEnum.PARAM_ERROR);
        }
        orderDTO.setOrderDetailList(orderDetailList);
        return orderDTO;
    }
}
